from django.shortcuts import render

# Create your views here.
from luffyapi.settings.dev import GEETEST
from rest_framework.views import APIView
from luffyapi.libs.geetest import GeetestLib
from rest_framework.response import Response
from rest_framework.generics import CreateAPIView
from rest_framework import status
# Create your views here.
class GeetestCapchaAPIView(APIView):
    def get(self, request):
        """生成验证码的流水号"""
        user_id = 'test'
        gt = GeetestLib(GEETEST["pc_geetest_id"], GEETEST["pc_geetest_key"])
        status = gt.pre_process(user_id)
        response_str = gt.get_response_str()
        import json
        response = json.loads(response_str)
        return Response(response)

    def post(self, request):
        """校验验证码的结果"""
        gt = GeetestLib(GEETEST["pc_geetest_id"], GEETEST["pc_geetest_key"])
        challenge = request.data.get(gt.FN_CHALLENGE, '')
        validate = request.data.get(gt.FN_VALIDATE, '')
        seccode = request.data.get(gt.FN_SECCODE, '')
        result = gt.failback_validate(challenge, validate, seccode)
        result = {"status": "success"} if result else {"status": "fail"}
        return Response(result)

from . import models
from .serializers import UserModelSerializer


class UserAPIView(CreateAPIView):
    # 用户的视图
    queryset = models.User.objects.all()
    serializer_class = UserModelSerializer

class SMSAPIView(APIView):
    '''短信发送接口'''
    def get(request,mobile):
        # 验证手机号是否注册
        try:
            from .models import User
            User.objects.get(mobile=mobile)
            return Response({"message": "对不起，当前手机已经被注册！"}, status=status.HTTP_400_BAD_REQUEST)
        except User.DoesNotExist:
            pass